package com.topsun.framework.common.core.util;


import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * 
 * @author: 皮皮
 * @date: 2022/4/23 9:54 上午
 */
public class FileUtil extends cn.hutool.core.io.FileUtil {

    public static String tmpFilePath() {
        return FileUtil.getTmpDirPath() + StringPool.SLASH + IdUtil.randomUUID() + ".tmp";
    }

    /**
     * 保存临时文件
     * @param fileBytes
     * @return
     */
    public static String saveTmpFile(byte[] fileBytes) throws IOException {
        String tmpFilePath = tmpFilePath();
        writeFromStream(new ByteArrayInputStream(fileBytes), new File(tmpFilePath));
        return tmpFilePath;
    }

    /**
     * 保存Url为临时文件
     * @param url
     * @return
     */
    public static String saveUrlTmpFile(String url) throws IOException {
        String tmpFilePath = tmpFilePath();
        writeFromStream(new URL(url).openStream(), FileUtil.file(tmpFilePath));
        return tmpFilePath;
    }
}
